package cn.com.godliu.oauth2.service;

import cn.com.godliu.oauth2.pojo.Account;
import cn.com.godliu.oauth2.pojo.CustomUserDetails;
import cn.com.godliu.oauth2.repositorys.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 自定义UserDetailsService，实现从数据库查询用户来完成token的获取
 */
@Service
public class AccountUserDetailsService implements UserDetailsService {

    @Autowired
    private AccountRepository accountRepository;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Account account = accountRepository.countAccountByUserName(s);
        return new CustomUserDetails(account);
    }
}
